<?php
	class Modstock extends CI_Controller
	{
		function index()
		{
			session_start();
			if(isset($_SESSION["host"]))
			{
				$this->load->view("modificar/iddisco");
			}
			else
			{
				redirect("/principal/index");
			}
		}
		function mstock()
		{
			session_start();
			if(isset($_SESSION["host"]))
			{
				$socket=fsockopen($_SESSION["host"],(int)$_SESSION["puerto"]);
				if($socket)
				{
					fputs($socket,$_SESSION["id"]);
					time_nanosleep(0,500000000);
					$idstock=str_pad($this->input->post("iddisco"),4,'0',STR_PAD_LEFT);
					fputs($socket,"modifs".$idstock);
					$msj=fread($socket,1024);
					if($msj=="Error")
					{
						echo "Error: ID no encontrada";
					}
					else
					{
					sscanf($msj,"%4[ -~]%4[ -~]",$id,$cantidad);
					echo "
						<form id='form' method='post' action='modstock/enviar'>
							<ul>
								<li>ID: <input type='text' readonly='readonly' name='iddisco' value='".$id."'/></li></br>
								<li>Cantidad: <input type='text' name='cantidad' maxlength='10' value='".$cantidad."'/></li></br>
							</ul>
							<input type='submit' value='Modificar'/>
						</form>
						  ";
					}
				}
				else
				{
					print_r("Error en conexion!");
				}
			}
			else
			{
				redirect("/principal/index");
			}
		}
		function enviar()
		{
			session_start();
			if(isset($_SESSION["host"]))
			{
				$socket=fsockopen($_SESSION["host"],(int)$_SESSION["puerto"]);
				if($socket)
				{
					fputs($socket,$_SESSION["id"]);
					time_nanosleep(0,500000000);
					$iddisco=str_pad($this->input->post("iddisco"),4,'0',STR_PAD_LEFT);
					$cantidad=str_pad($this->input->post("cantidad"),4,' ',STR_PAD_LEFT);
					fputs($socket,"stock2".$iddisco.$cantidad);
					redirect("/modificar/modstock");
				}
				else
				{
					print_r("Error en conexion!");
				}
			}
			else
			{
				redirect("/principal/index");
			}
			
		}
	}
?>